package com.starbaba.weixin.view;

import java.util.ArrayList;
import java.util.List;

import com.base.common.database.BaseDao;
import com.base.common.log.LogService;
import com.opensymphony.xwork2.ActionSupport;
import com.starbaba.comment.model.EntityVO;
import com.starbaba.core.service.CommonService;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.weixin.constants.WeixinMsgConstant;
import com.starbaba.weixin.model.WeixinReportVO;
public class WeixinMsgReportAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2669857780100038437L;
	
	private List<EntityVO> bussinessEntityVOs;
	
	public String showPage() {
		getBusinessCategory();
		return ActionSupport.SUCCESS;
	}

	public void getMsgReport() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		String startTime = (String) paramsMap.get("startTime");
		String endTime = (String) paramsMap.get("endTime");
		String business = (String) paramsMap.get(WeixinMsgConstant.BUSINESS);
		parsePageRequest();
		try {
			PageResponse pageResponse = new PageResponse();
			BaseDao dao = new BaseDao(Constant.POOL_LIFE);
			// 查询批量数据
			StringBuffer sbSql = new StringBuffer();
			sbSql.append("select DATE_FORMAT(weixin_mpmsg_record.date,'%Y-%m-%d') day_no");
			sbSql.append(",COUNT(*) as total_cnt, business as busi from weixin_mpmsg_record where id != 0 ");
			if(startTime != null){
				sbSql.append(" and weixin_mpmsg_record.date>= '" + startTime +"'");
	    	}
	    	if(endTime != null){
	    		sbSql.append(" and weixin_mpmsg_record.date<= '" + endTime +" 23:59:59'");
	    	}
	    	if (business != null) {
	    		if (business.equals("未处理业务")) {
					business = "";
				}
	    		sbSql.append(" and weixin_mpmsg_record.business like '" + business + "'");
	    	}
			sbSql.append(" GROUP BY day_no");
			sbSql.append(" ORDER BY day_no DESC");
			
	    	int pageSize = pageRequest.getIDisplayLength();
	    	int pageIndex = pageRequest.getIDisplayStart();
	    	String limitSql = sbSql.toString() + " LIMIT " + pageIndex + ", " + pageSize;
			List dataList = dao.list(limitSql, WeixinReportVO.class);
			
			
			pageResponse.setAaData(dataList);
			pageResponse.setSEcho(pageRequest.getSEcho());
			List tmpDataList = dao.list(sbSql.toString(), WeixinReportVO.class);
			pageResponse.setITotalDisplayRecords(tmpDataList.size());
			pageResponse.setITotalRecords(tmpDataList.size());
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
			LogService.error("getGeneraldata", e);
		}		
	}
	
	private void getBusinessCategory() {
		StringBuffer sb = new StringBuffer();
		sb.append("select distinct business from loan.weixin_mpmsg_record");
		CommonService service = getService();
		List businessCategoryList = service.doFindBySQL(sb.toString(),
				null, null);
		bussinessEntityVOs =  new ArrayList<EntityVO>();
		for (int i = 0; i < businessCategoryList.size();i++) {
			String s = (String) businessCategoryList.get(i);
			String key = s;
			if (s.equals("")) {
				s = "未处理业务";
				key = "未处理业务";
			}
			EntityVO entityVO = new EntityVO(key, s);
			bussinessEntityVOs.add(entityVO);
		}
		paramsMap.put(WeixinMsgConstant.BUSINESS_LIST, bussinessEntityVOs);
	}
	
	
}
